package com.example.demo.poi.simple;

import com.alibaba.excel.EasyExcel;
import com.example.demo.poi.CustomCellWriteHandler;
import com.example.demo.vo.EasyExcelVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SimpleEasyExcelController {

    /**
     *
     */
    @GetMapping("/simple/test")
    public void test(HttpServletResponse response) throws IOException {
        List<SimpleEasyExcelVo> data = new ArrayList<>();


        SimpleEasyExcelVo simpleEasyExcelVo = new SimpleEasyExcelVo();
        simpleEasyExcelVo.setSurname("王");
        simpleEasyExcelVo.setName("汉");
        simpleEasyExcelVo.setAge(18);
        simpleEasyExcelVo.setHeight(180.0);
        simpleEasyExcelVo.setSex("男");
        simpleEasyExcelVo.setPhone("158xxxxxxxx");
        simpleEasyExcelVo.setAddress("北京");
        data.add(simpleEasyExcelVo);

        SimpleEasyExcelVo simpleEasyExcelVo1 = new SimpleEasyExcelVo();
        simpleEasyExcelVo1.setSurname("王");
        simpleEasyExcelVo1.setName("汉");
        simpleEasyExcelVo1.setAge(18);
        simpleEasyExcelVo1.setHeight(180.0);
        simpleEasyExcelVo1.setSex("男");
        simpleEasyExcelVo1.setPhone("158xxxxxxxx");
        simpleEasyExcelVo1.setAddress("北京");
        data.add(simpleEasyExcelVo1);

        SimpleEasyExcelVo simpleEasyExcelVo2 = new SimpleEasyExcelVo();
        simpleEasyExcelVo2.setSurname("王");
        simpleEasyExcelVo2.setName("汉");
        simpleEasyExcelVo2.setAge(18);
        simpleEasyExcelVo2.setHeight(180.0);
        simpleEasyExcelVo2.setSex("男");
        simpleEasyExcelVo2.setPhone("158xxxxxxxx");
        simpleEasyExcelVo2.setAddress("北京");
        data.add(simpleEasyExcelVo2);

        SimpleEasyExcelVo simpleEasyExcelVo3 = new SimpleEasyExcelVo();
        simpleEasyExcelVo3.setSurname("王");
        simpleEasyExcelVo3.setName("汉");
        simpleEasyExcelVo3.setAge(18);
        simpleEasyExcelVo3.setHeight(180.0);
        simpleEasyExcelVo3.setSex("男");
        simpleEasyExcelVo3.setPhone("158xxxxxxxx");
        simpleEasyExcelVo3.setAddress("北京");
        data.add(simpleEasyExcelVo3);


        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("文本素材导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SimpleEasyExcelVo.class).sheet("模板").doWrite(data);
    }
}
